var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var circleUrl = require("url/circle");
var joinCirclesUrl = circleUrl.url.base.myCircles;
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var listView = ui("listView");


var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "选择成员",
	"labelNext" : "确认"
})
header.refreshData();

var circleId = null;
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	circleId = do_Page.getData().circleId;
	laodPage();
});
listView.bindItems(listdata);
// 刷新数据
// 刷新数据
var page;
function laodPage() {
	page = 1;
	var params = {
		page : page,
		circleId: circleId
	};
	qqHttp.post(circleUrl.url.member.members4NoGrade,params, function(data) {
		listView.rebound();
		listdata.removeAll();
		listdata.addData(data.memberList);
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		circleId: circleId
	};
	qqHttp.post(circleUrl.url.member.members4NoGrade, params, function(data) {
		listView.rebound();
		listdata.addData(data.memberList);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		laodPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
do_Page.on("check_change_event", function(d) {
	//deviceone.print(JSON.stringify(d));
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].id==d.id){
			alldata[i].checked=d.checked;
			listdata.updateOne(i, alldata[i]);
			break;
		}
	}
})
headerRight.on("touch", function() {
	var members = [];
	var alldata = listdata.getRange(0);
	for(var i = 0;i<alldata.length;i++){
		if(alldata[i].checked){
			members.push(alldata[i]);
		}
	}
	do_App.closePage({"type":"selectCircleMember","members":members})
})
